package com.pz.gulimall.auth.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.alibaba.fastjson2.JSON;
import com.pz.gulimall.auth.service.OrderService;
import com.pz.gulimall.auth.vo.User;
import com.pz.gulimall.common.to.OrderInfoTo;
import com.pz.gulimall.common.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("user/order")
public class OrderController {
    @Autowired
    private OrderService orderService;

    /**
     * 获取商品信息
     */
    @PostMapping("/get-skus")
    public R getSkuProducts(@RequestBody List<Long> skus){
        return orderService.getProducts(skus);
    }

    /**
     * 保存订单
     * @return {data: orderId}
     */
    @PostMapping("/add-newOrder")
    public R inItOrder(@RequestBody OrderInfoTo orderInfoTo){
        orderInfoTo.setMemberId(getMemberId());
        return orderService.saveOrder(orderInfoTo);
    }

    /**
     * 获取当前用户所有订单数据
     * @param params {status,currentPage, limit}
     */
    @GetMapping("/getOrders")
    public R getOrders(@RequestParam Map<String, Object> params){
        params.put("memberId", getMemberId());
        return orderService.getOrders(params);
    }


    @GetMapping("/getOrderDetail/{orderId}")
    public R getOrderDetail(@PathVariable Long orderId) {
        Map<String, Object> params = new HashMap<>();
        params.put("memberId", getMemberId());
        params.put("orderId", orderId);
        return orderService.getOrders(params);
    }

    private long getMemberId(){
        return JSON.to(User.class, StpUtil.getSession().get("user")).getId();
    }
}
